package com.mcptt.config.ue.config;

import com.cloudptt.api.product.log.Log;
import com.mcptt.config.AbstractConfigFile;
import com.mcptt.config.callback.IParserCallback;
import com.mcptt.config.ue.config.sub.Common;
import com.mcptt.config.ue.config.sub.OnNetwork;
import com.mcptt.config.ue.initial.sub.McpttUeId;
import com.mcptt.db.DBAdapter;
import com.mcptt.db.model.ConfigFileInfo;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

public class UeConfig extends AbstractConfigFile
{
//    public static final int FileType = 2;

	public static final String FileName = "org.3gpp.mcptt.ue-config";
	private static final String FilePath = "//config//";
	public static final String Name = "mcptt-UE-configuration";
	private static final String Name_Domain = "domain";
	// private static final String Name_SchemaLocation = "schemaLocation";
	// private static final String Name_Xsi = "xmlns:xsi";
	// private static final String Name_Xmlns = "xmlns";
	private static final String Name_Name = "name";

	private String domain = null;
	// private String schemaLocation = null;
	// private String xsi = null;
	// private String xmlns = null;

	private McpttUeId mcpttUeId = null;
	private String name = null;
	private Common common = null;
	private OnNetwork onNetwork = null;
	private IParserCallback parserCallback = null;

//    private void notifyFailed(IParserCallback callback,String errStr)
public UeConfig(DBAdapter dbOperator, IParserCallback parserCallback)
{
	super(dbOperator);
	this.parserCallback = parserCallback;
//	this.info = readFromDb(FileName);
//	parse(info);
}
//    {
//	if(callback != null)
//	{
//	    callback.parserNotifyFailed(errStr);
//	}
//    }

	public void parse(ConfigFileInfo info)
	{
		if(info == null)
		{
			return;
		}
		SAXBuilder builder = new SAXBuilder();// 实锟斤拷JDOM锟斤拷锟斤拷锟斤拷
//	File file = new File(Environment.getExternalStorageDirectory()
//		+ FilePath, FileName);
//	FileInputStream fis = new FileInputStream(file);
		String content = info.getContent();
		ByteArrayInputStream strIs = new  ByteArrayInputStream(content.getBytes());
		try {
			Document document = builder.build(strIs);
			Element root = document.getRootElement();// 锟斤拷酶锟斤拷诘锟�<bookstore>
			if (root == null)
			{
				return;
			}
			domain = root.getAttributeValue(Name_Domain);
			// Namespace ns = root.getNamespace();
			// schemaLocation = root.g.getAttributeValue(Name_SchemaLocation, ns);
			// schemaLocation = root.getAttributeValue(Name_SchemaLocation);
			// xsi = root.getAttributeValue(Name_Xsi);
			// xmlns = root.getAttributeValue(Name_Xmlns);

			List<Element> list = root.getChildren();// 锟斤拷酶锟斤拷诘锟斤拷锟接节碉拷
			for (Element elem : list)
			{
				String name = elem.getName();
				Log.debug(this.getClass(), "name = " + name);
				if (name == null || name.length() <= 0)
				{
					return;
				}
				if (name.equals(McpttUeId.Name))
				{
					mcpttUeId = new McpttUeId();
					mcpttUeId.parse(elem);
				} else if (name.equals(Name_Name))
				{
					this.name = elem.getText();
				} else if (name.equals(Common.Name))
				{
					common = new Common();
					common.parse(elem);
				} else if (name.equals(OnNetwork.Name))
				{
					onNetwork = new OnNetwork();
					onNetwork.parse(elem);
				}
			}
		}catch (Exception e)
		{
			e.printStackTrace();
			notifyFailed(parserCallback,FileName,"UeConfig parse Exception = " + e);
		}

	}

	public boolean checkMustParamOk()
	{
		return true;
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<").append(Name);
		strBuf.append(" ").append(Name_Domain).append("=\"").append(domain)
				.append("\" ");
		// strBuf.append(" ").append(Name_SchemaLocation).append("=\"").append(schemaLocation).append("\" ");
		// strBuf.append(" ").append(Name_Xsi).append("=\"").append(xsi).append("\" ");
		// strBuf.append(" ").append(Name_Xmlns).append("=\"").append(xmlns).append("\" ");
		strBuf.append(">");

		strBuf.append(mcpttUeId);
		strBuf.append("    ").append("<").append(Name_Name).append(">");
		strBuf.append(name);
		strBuf.append("</").append(Name_Name).append(">").append("\r\n");
		strBuf.append(common);
		strBuf.append(onNetwork);
		strBuf.append("</").append(Name).append(">");

		return strBuf.toString();
	}

	@Override
	public void syncToSvr()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void saveInfoToDb()
	{
		// TODO Auto-generated method stub

	}
}
